// 格式化date类型输出,输出格式2020-03-23 15:30
function formatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()


  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute].map(formatNumber).join(':')
}
// 格式化date类型输出,输出格式 03-23 15:30
function formatTimeWithoutYear(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()
  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()
  return [month, day].map(formatNumber).join('-') + ' ' + [hour, minute].map(formatNumber).join(':')
}
// 格式化date类型输出,输出格式 2020-03-23
function formatTimeWithoutHour(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()
  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()
  return [month, day].map(formatNumber).join('-')
}
// 格式化返回的地址
function formatAddress(address) {
  var parseAddress = JSON.parse(address)
  return parseAddress

}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}
// 格式化新闻的摘要，超过200字用...代替
function subText(text) {
  if (typeof(text) == "string" && text.length > 200) {
    return text.substring(0, 200) + "..."
  }
  return text
}
// 格式化省份
function formatProvince(list) {
  var provinces = ["北京市", "天津市", "上海市", "重庆市", "河北省", "山西省", "辽宁省", "吉林省", "黑龙江省", "江苏省",
    "浙江省", "安徽省", "福建省", "江西省", "山东省", "河南省", "湖北省", "湖南省", "广东省", "海南省",
    "四川省", "贵州省", "云南省", "陕西省", "甘肃省", "青海省", "台湾省", "内蒙古自治区", "广西壮族自治区", "西藏自治区", "宁夏回族自治区", "新疆维吾尔自治区", "香港特别行政区", "澳门特别行政区"
  ]
  var tmp=[]
  for (let j =0;j<34;j++){
    tmp[j]={}
    tmp[j].province = provinces[j]
    tmp[j].curNum = 0
    tmp[j].totalNum = 0
    tmp[j].deadNum = 0
    tmp[j].curedNum = 0
    tmp[j].cities = []
  }
  // console.log(tmp)
  var list = JSON.parse(JSON.stringify(list).replace(/current_diagnosis/g, "curNum").replace(/total_diagnosis/g, "totalNum").replace(/total_death/g, "deadNum").replace(/total_recover/g, "curedNum").replace(/city_name/g, "city"));
  // console.log(list)
  for (let i = 0; i < list.length; i++) {
    for (let j = 0; j < 34; j++) {
      if (tmp[j].province == list[i].byprovince){
        tmp[j].cities.push(list[i])
      }
    }
  }
  var list2 = JSON.parse(JSON.stringify(tmp).replace(/province/g, "firstCol").replace(/cities/g, "second").replace(/city/g, "secondCol"));
  // console.log(list)
  return list2
}
// 格式化性别数据
function formatGender(list){
  var list2 = JSON.parse(JSON.stringify(list).replace(/gender/g, "firstCol").replace(/人数/g, "totalNum"));
 return list2
}
// 格式化年龄段数据
function formatAge(list) {
  // console.log(list)
  var list1=[]
  var num=0
  for (var i in list){
    list1[num]={}
    list1[num].firstCol=i
    list1[num].totalNum = list[i]
    num++
  }
  // console.log(list1)
  return list1
}
// 格式化热力地图数据
function formatMap(list) {
  // console.log(list)
  var list1 = []
  for (let i=0;i<list.length;i++) {
    var value = list[i].currentDiagnosis
    if (value>0){
      list1[i] = {}
      list1[i].name = list[i].provinceName
      list1[i].value = value
    }
   
  }
  // console.log(list1)
  return list1
}
module.exports = {
  formatTime: formatTime,
  formatTimeWithoutYear: formatTimeWithoutYear,
  subText: subText,
  formatTimeWithoutHour: formatTimeWithoutHour,
  formatAddress: formatAddress,
  formatProvince: formatProvince,
  formatGender: formatGender,
  formatAge: formatAge,
  formatMap: formatMap
};